﻿var g;
var mainData;
var itemData;

$(function () {
    $("#ddlPatientList").ligerComboBox({
        width: 140,
        cancelable: false,
        valueField: "PATIENT_ID",
        textField: "PATIENT_NAME",
        valueFieldID: 'hdnPatientId',
        onSelected: function (value, text) {
            $.ajax({
                url: "/StrokeCenter/InspectionResult/Search?ram" + Math.random(),
                type: "Post",
                data: { triageId: $("#hdnPatientId").val() },
                success: function (result) {
                    var jsonResult = $.parseJSON(result);
                    mainData = jsonResult.mainData;
                    itemData = jsonResult.itemData;
                    f_showMain();
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
    getPatientList();
});

function getPatientList() {
    $.ajax({
        url: "/StrokeCenter/PatientList/GetPatientList?ram" + Math.random(),
        type: "Post",
        data: {},
        success: function (result) {
            var jsonResult = $.parseJSON(result);
            if (jsonResult.Result == 1) {
                var jsonData = $.parseJSON(jsonResult.Body).PatientList;
                for (var i = 0; i < jsonData.length; i++) {
                    jsonData[i].PATIENT_NAME = jsonData[i].PATIENT_NAME + "   " + (jsonData[i].PATIENT_SEX == 1 ? "男" : jsonData[i].PATIENT_SEX == 0 ? "女" : "") + "    " + (IsEmpty(jsonData[i].PATIENT_AGE) ? "" : jsonData[i].PATIENT_AGE + "岁");
                }
            }
            $("#ddlPatientList").ligerComboBox({ data: jsonData });
            $("#ddlPatientList").ligerComboBox().setValue(jsonData[0].PATIENT_ID);

            $(".l-box-select-table").find("tr").each(function () {
                var reg = new RegExp(" ", "g");
                $(this).children('td').eq(0).first("span").html($(this).children('td').eq(0).first("span").html().replace(reg, "&nbsp;"));
            });
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

//显示主表
function f_showMain() {
    g = $("#maingrid").ligerGrid({
        height: 'auto',
        heightDiff: -5,
        pageSize: 30,
        usePager: false,
        isScroll: false,
        fixedCellHeight: false,
        columns: [
        { display: '检验编号', name: 'reportid', width: 150 },
        {
            display: '检验日期', name: 'sampledate', width: 120, render: function (row) {
                return row.sampledate.split(' ')[0];
            }
        },
        { display: '标本名称', name: 'specimen_name', width: 120 },
        { display: '检查项目', name: 'req_reason', width: 300 },
        {
            display: '检验状态', name: 'emer_flag', width: 100, render: function (row) {
                if (row.emer_flag == 1) {
                    return "<font style=\"color:red\">加急</font>";
                }
                else {
                    return "";
                }
            }
        }],
        data: mainData,
        detailHeight: 'auto',
        detail: { onShowDetail: f_showItem }
    });
}

//显示子表
function f_showItem(row, detailPanel, callback) {
    var grid = document.createElement('div');
    $(detailPanel).append(grid);
    $(grid).css('margin', 10).ligerGrid({
        columns:
                    [
                    { display: '检查名称', name: 'rpt_itemname', width: 150 },
                    { display: '检查结果', name: 'result_str' },
                    { display: '单位', name: 'result_unit' },
                    { display: '参考值', name: 'result_ref' },
                    { display: '结论', name: 'result_flag' }
                    ],
        enabledSort: false,
        columnWidth: 120,
        isScroll: false,
        showToggleColBtn: false,
        data: f_getOrdersData(row.reportid),
        showTitle: false,
        onAfterShowData: callback,
        frozen: false
    });
}

function f_getOrdersData(reportid) {
    var data = { Rows: [] };
    for (var i = 0; i < itemData.Rows.length; i++) {
        if (itemData.Rows[i].reportid == reportid)
            data.Rows.push(itemData.Rows[i]);
    }
    return data;
}
